package com.myh.cc.coom.singer.viewmodel

import android.content.Intent
import android.view.View
import com.blankj.utilcode.utils.LogUtils.i
import com.myh.cc.base.BaseModel
import com.myh.cc.coom.singer.adapter.SingerAdapter
import com.myh.cc.coom.singer.adapter.TypeAdapter
import com.myh.cc.coom.singer.bean.SingerBean
import com.myh.cc.coom.singer.bean.TypeBean
import com.myh.cc.coom.singer.net.SingerNetControl
import com.myh.cc.coom.singer.view.SingerDetailsActivity
import com.myh.cc.coom.singer.view.SingerFragment
import com.myh.cc.databinding.FragmentSingerBinding
import com.myh.cc.util.ConstantUtil
import com.myh.cc.widget.OnItemClickListener

/**
 * des    ：
 * create    ：  mayahao.
 * date   ：  2018/1/14.   ：
 */
class SingerModel:BaseModel<FragmentSingerBinding,SingerNetControl>(),OnItemClickListener {


    lateinit var  fragment: SingerFragment
    lateinit var bean: SingerBean

    override fun onCreate() {
        super.onCreate()
        fragment = UI as SingerFragment
    }

    override fun onBeforeRequest(tag: Int) {
    }

    override fun onSuccess(data: Any, tag: Int) {
        bean = data as SingerBean
        initRecycleView(bean)
    }

    override fun onError(errorMag: String, tag: Int) {
        i(errorMag)
    }



    /**
     * 请求
     */
    fun getSinger(type: String,abc: String){
        // 这样写 有点蠢
        val key = (mBinder.singerRvType.adapter as TypeAdapter).keyType+"_"+(mBinder.singerRvAbc.adapter as TypeAdapter).keyAbc

        var map = HashMap<String,String>()

        map.put("format","json")
        map.put("hostUin","0")
        map.put("g_tk","5381")
        map.put("pagenum","1")
        map.put("pagesize","100")
        map.put("key",key)
        map.put("page","list")
        map.put("channel","singer")


        mControl.getSinger(this,"",map,ConstantUtil.requestTagOne)
    }

    /**
     * 数据请求成功后 给data 赋值
     */
    private fun initRecycleView(bean: SingerBean){
        var adapter = SingerAdapter(bean)
        mBinder.singerRvList.adapter = adapter
        adapter.mListener =this

    }


    fun initRecycleViewType() {

        val bean = ArrayList<TypeBean>()
        bean.add(TypeBean("all_all","全部"))
        bean.add(TypeBean("cn_man","华语男"))
        bean.add(TypeBean("cn_woman","华语女"))
        bean.add(TypeBean("cn_team","华语组合"))
        bean.add(TypeBean("k_man","日本男"))
        bean.add(TypeBean("j_woman","日本女"))
        bean.add(TypeBean("eu_man","欧美男"))
        bean.add(TypeBean("eu_woman","欧美女"))
        bean.add(TypeBean("eu_team","全部"))
        bean.add(TypeBean("eu_team","欧美组合"))


        mBinder.singerRvType.adapter = TypeAdapter(bean,this)

        mBinder.singerRvAbc.adapter = TypeAdapter(getAdcBean(),this)

    }



    fun getAdcBean(): List<TypeBean>{


        val  abc = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"

        val bean = ArrayList<TypeBean>()
        bean.add(TypeBean("all_all","全部"))

        for ( a in abc.split("")){
            if (a != ""){
                bean.add(TypeBean(a, a))
            }
        }

        return  bean
    }

    override fun onClick(view: View, position: Int) {
        val  intent = Intent(getContext(),SingerDetailsActivity::class.java)
        intent.putExtra("singermid",bean.data.list[position].Fsinger_mid)
        fragment.startActivity(intent)
    }

    override fun onItemLongClick(view: View, position: Int) {

    }


}